var isUpPlatformInfo = window.isUpPlatformInfo;
var jgpt = window.jgpt;
var offset = 0;
var limit = 20;
var pagingLimit = 20;// 显示分页时的条数
var currentTotalElements = 0;// 当前显示总条数
var pageLoading = false;// 是否能滚动加载
var hasShowPages = false;// 是否已显示分页
var firstLoadPages = true;// 分页初始化时 不自动加载
// 筛选条件,
var search_carNumber = null;
var search_employName = null;
var search_companyName = null;
var search_carType = null;
var search_status = [];
var search_isPlatform = [];
var upload_car = [];
$(function() {
	// 高级搜索
	initdata(false);
	$(".query-jiant").click(function() {
		var src = $("#jiant").attr("src");
		if (src.indexOf("jiant2") >= 0) {
			$("#jiant").attr("src", ctx + "/assets/img/jiant.png");
			$(".choose").show(300);
		} else {
			$("#jiant").attr("src", ctx + "/assets/img/jiant2.png");
			$(".choose").hide(300);
		}
	});
	$("#searchButton").click(function() {
		search_carNumber = $("#carNumber1").val();
		search_employName = $("#employName1").val();
		search_companyName = $("#companyName1").val();
		search_carType = $("#carType1").val();
		var statusArr = [];
		$("input[name=status]:checked").each(function(index, element) {
			statusArr.push($(element).val());
		});
		if (null != statusArr && statusArr.length > 0) {
			search_status = statusArr.join(",");
		} else {
			search_status = [];
		}

		var isPlatformArr = [];
		$("input[name=isPlatform]:checked").each(function(index, element) {
			isPlatformArr.push($(element).val());
		});
		if (null != isPlatformArr && isPlatformArr.length > 0) {
			search_isPlatform = isPlatformArr.join(",");
		} else {
			search_isPlatform = [];
		}

		offset = 0;
		initdata(false);
	});
	$(document).on("keydown", "body", function() {
		if (window.event.keyCode == 13) {
			$("#searchButton").click();
		}
	});

});

$(document).ready(function() {
	$("#all").click(function(event) {
		var flag = Boolean.parse($("#all").attr("val"));
		if (flag) {$(this).attr("val", false);
			var items = $(".checkCarItem");
			for (var i = 0, val; val = items[i++];) {
				$(val).removeAttr("checked");
				$(val).attr("flag", false);
				upload_car.remove(parseInt($(val).val()));
			}
		} else {
			$(this).attr("val", true);
			var items = $(".checkCarItem");
			for (var i = 0, val; val = items[i++];) {
				$(val).prop("checked", "true");
				$(val).attr("flag", true);
				upload_car.remove(parseInt($(val).val()));
				upload_car.push(parseInt($(val).val()));
			}
		}
		console.debug(upload_car);
		$(".query-jiant").append(
						"<span class='label label-info checkItemInfoLable' style='position: absolute;right: 80px;margin-top: 10px;'>共选中 : "
								+ upload_car.length
								+ "条</span>");
		event.stopPropagation();
	});
	$("#save").click(function() {
		if (isMainCompany
				&& !$('#companyId').val()) {
			$.scojs_message("请选择公司",
					$.scojs_message.TYPE_ERROR);
			return false;
		}
		if (!$('#carName').val().trim()) {
			$.scojs_message("请输入车辆名称",
					$.scojs_message.TYPE_ERROR);
			return false;
		} else if ($('#carName').val().trim().length > 10) {
			$.scojs_message("车辆名称过长",
					$.scojs_message.TYPE_ERROR);
			return false;
		}
		var carNumber = $('#carNumber').val();
		if (!carNumber.trim()) {
			$.scojs_message("请输入车牌号码",
					$.scojs_message.TYPE_ERROR);
			return false;
		} else {
            if(!(/^([\u4e00-\u9fa5]{1}[A-z]{1}[A-z_0-9]{5,10})$/.test(carNumber))){
            $.scojs_message("车牌号格式不正确", $.scojs_message.TYPE_ERROR);
            return false;
        }
		}
		//童俊超添加倒班班次判断
	  	var flights = $('#flights').val();
	  	if(null==flights && flights == ""){
	  		$.scojs_message("请填写倒班班次", $.scojs_message.TYPE_ERROR);
	  		return false;
	  	}else if(flights.trim().length>5){
	  		$.scojs_message("倒班班次过长", $.scojs_message.TYPE_ERROR);
	  		return false;
	  	}
		$.get(ctx+ '/zhuanche/queryCarByNumber',{'carNumber' : carNumber,'flights':flights},function(data) {
			if (data.code == 0) {
				$.scojs_message(data.message,$.scojs_message.TYPE_ERROR);
				return false;
			} else {
				var distance = $('#drivingDistance').val();
				if (!distance.trim()) {
					$.scojs_message("请输入行驶里程",$.scojs_message.TYPE_ERROR);
					return false;
				} else if (Number(distance) < 0
						|| Number(distance) > 1000000) {
					$.scojs_message("行驶里程输入错误",$.scojs_message.TYPE_ERROR);
					return false;
				}
				if (!$('#buyTime').val()) {
					$.scojs_message("请选择购车日期",$.scojs_message.TYPE_ERROR);
					return false;
				}
				if (!$('#overhaulTime').val()) {
					$.scojs_message("请选择年检日期",$.scojs_message.TYPE_ERROR);
					return false;
				}
				if (!$('#insuranceTime').val()) {
					$.scojs_message("请选择保险到期日期",$.scojs_message.TYPE_ERROR);
					return false;
				}
				// 当监管平台开关打开时
				if (isUpPlatformInfo == 'true') {
					if (!$('#insurer').val()) {
						$.scojs_message("上传监管平台开关已开启，请填写车辆保险公司",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#insuranceNo').val()) {
						$.scojs_message("上传监管平台开关已开启，请填写车辆保险号",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#insType').val()) {
						$.scojs_message("上传监管平台开关已开启，请填写车辆保险类型",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#insMoney').val()) {
						$.scojs_message("上传监管平台开关已开启，请填写车辆保险金额",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#insValidTime').val()) {
						$.scojs_message("上传监管平台开关已开启，请填写车辆保险生效日期",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#plateColor').val()) {
						$.scojs_message("上传监管平台开关已开启，请填写车牌颜色，如蓝色",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#brand').val()) {
						$.scojs_message("上传监管平台开关已开启，请填写车辆厂牌，如奔驰牌",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#brand').val()) {
						$.scojs_message("上传监管平台开关已开启，请填写车牌名称，如奔驰牌",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#vehicleType').val()) {
						$.scojs_message("上传监管平台开关已开启，请填写车辆类型，如小型轿车",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#vehicleModel').val()) {
						$.scojs_message("上传监管平台开关已开启，请填写车辆型号，如E643133",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#owner').val()) {
						$.scojs_message("上传监管平台开关已开启，请填写所属车主",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#bodyColor').val()) {
						$.scojs_message("上传监管平台开关已开启，请填写车身颜色",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#engineNo').val()) {
						$.scojs_message("上传监管平台开关已开启，请填写发动机号",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#vin').val()) {
						$.scojs_message("上传监管平台开关已开启，请填写车辆识别VIN",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#fuel').val()) {
						$.scojs_message("上传监管平台开关已开启，请填写燃料类型，如：A（汽油）",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#permitted').val()) {
						$.scojs_message("上传监管平台开关已开启，核定载客不能为空",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#registrationTime').val()) {
						$.scojs_message("上传监管平台开关已开启，请填写车辆注册日期",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#displacement').val()) {
						$.scojs_message("上传监管平台开关已开启，请填写车辆排量",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#address').val()) {
						$.scojs_message("上传监管平台开关已开启，请填写详细地址",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#taxiAgency').val()) {
						$.scojs_message("上传监管平台开关已开启，请填写运输证发证机构",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#taxiRegion').val()) {
						$.scojs_message("上传监管平台开关已开启，请填写运输证经营区域",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#taxiNo').val()) {
						$.scojs_message("上传监管平台开关已开启，请填写运输证编号",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#rateType').val()) {
						$.scojs_message("上传监管平台开关已开启，运价类型不能为空 ",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#taxiRegisterTime').val()) {
						$.scojs_message("上传监管平台开关已开启，请填写运输证激活日期",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#validTime').val()) {
						$.scojs_message("上传监管平台开关已开启，请填写运输证有效日期",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#invalidTime').val()) {
						$.scojs_message("上传监管平台开关已开启，请填写运输证失效日期",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#deviceSerial').val()) {
						$.scojs_message("上传监管平台开关已开启，发票打印设备序号不能为空",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#deviceBrand').val()) {
						$.scojs_message("上传监管平台开关已开启，定位装置品牌不能为空",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#deviceModel').val()) {
						$.scojs_message("上传监管平台开关已开启，定位装置型号不能为空",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#devicegpsIMEI').val()) {
						$.scojs_message("上传监管平台开关已开启，定位装置IMEI不能为空",$.scojs_message.TYPE_ERROR);
						return false;
					}
					if (!$('#deviceInstallTime').val()) {
						$.scojs_message("上传监管平台开关已开启，定位装置安装日期不能为空",$.scojs_message.TYPE_ERROR);
						return false;
					}
					// 车辆照片是否为空
					var carImg = $("#carImg")[0].src;
					var flag = carImg.indexOf("noImg.png");
					if (CheckImgExists(carImg)) {
						if (flag != -1) {
							$.scojs_message("上传监管平台开关已开启，请上传车辆照片",$.scojs_message.TYPE_ERROR);
							return false
						}
					} else {
						$.scojs_message("上传监管平台开关已开启，请上传车辆照片",$.scojs_message.TYPE_ERROR);
						return false
					}
				}
				$.post(ctx+ "/zhuanche/createCar",$("#submit_form").serialize(),function(data) {
					if (data.success) {
						$.scojs_message("创建成功",$.scojs_message.TYPE_OK);
						setTimeout(function() {
							window.location.reload(true);
						},800);
					} else {
						$.scojs_message(data.message,$.scojs_message.TYPE_ERROR);
					}
				},'json');
			}
		}, 'json');
	});

	$("#car_insurance_save").click(function() {
		var insuranceTime = $('#insuranceTime3')
				.val();
		if (!insuranceTime) {
			$.scojs_message("请选择到期保险日期",
					$.scojs_message.TYPE_ERROR);
			return false;
		} else {
			if ((new Date(insuranceTime.replace("-", "/")).getTime())- (new Date().getTime()) < 0) {
				$.scojs_message("保险到期日期不能在当前日期之前",$.scojs_message.TYPE_ERROR);
				return false;
			}
		}
		if (!$('#insuranceCompany').val()) {
			$.scojs_message("请填写保险公司",$.scojs_message.TYPE_ERROR);
			return false;
		}
		if (!$('#insuranceCode').val()) {
			$.scojs_message("请填写保险号",$.scojs_message.TYPE_ERROR);
			return false;
		}
		if (!$('#insuranceType').val()) {
			$.scojs_message("请填写保险类型",$.scojs_message.TYPE_ERROR);
			return false;
		}
		if (!$('#insuranceAmount').val()) {
			$.scojs_message("请填写保险金额",$.scojs_message.TYPE_ERROR);
			return false;
		} else if (isNaN($('#insuranceAmount')
				.val())) {
			$.scojs_message("保险金额请填写数字",$.scojs_message.TYPE_ERROR);
			return false;
		}
		if (!$('#insValidTime2').val()) {
			$.scojs_message("请选择保险生效日期",
					$.scojs_message.TYPE_ERROR);
			return false;
		}
		$.post(ctx+ '/zhuanche/createInsurance',$('#insurance_from').serialize(),
			function(data) {
				if (data.success) {
					$.scojs_message("修改成功",$.scojs_message.TYPE_OK);
					setTimeout(function() {
						window.location.reload(true);
					}, 800);
				} else {
					$.scojs_message(data.message,$.scojs_message.TYPE_ERROR);
				}
			}, 'json');
		});
					$("#update").click(function() {
						if (isMainCompany&& !$('#companyId2').val()) {
							$.scojs_message("请选择公司",$.scojs_message.TYPE_ERROR);
							return false;
						}
						if (!$('#carName2').val().trim()) {
							$.scojs_message("请输入车辆名称",$.scojs_message.TYPE_ERROR);
							return false;
						} else if ($('#carName2').val().trim().length > 10) {
							$.scojs_message("车辆名称过长",$.scojs_message.TYPE_ERROR);
							return false;
						}
						var carNumber = $('#carNumber2').val();
						if (!carNumber.trim()) {
							$.scojs_message("请输入车牌号",
									$.scojs_message.TYPE_ERROR);
							return false;
						} else {
							if (!(/^([\u4e00-\u9fa5]{1}[A-z]{1}[A-z_0-9]{5,10})$/.test(carNumber))) {
								$.scojs_message("车牌号格式不正确",$.scojs_message.TYPE_ERROR);
								return false;
							}
						}
						//童俊超添加倒班班次判断
					  	var flights = $('#flights2').val();
					  	if(null==flights && flights == ""){
					  		$.scojs_message("请填写倒班班次", $.scojs_message.TYPE_ERROR);
					  		return false;
					  	}else if(flights.trim().length>5){
					  		$.scojs_message("倒班班次过长", $.scojs_message.TYPE_ERROR);
					  		return false;
					  	}

						$.get(ctx+ '/zhuanche/queryCarByNumber',{'carNumber' : carNumber,'flights':flights},function(data) {
							if (data.code == 0&& data.data != null&& data.data.id != $("#carId").val()) {
								$.scojs_message(data.message,$.scojs_message.TYPE_ERROR);
								return false;
							} else {
								var distance2 = $('#drivingDistance2').val();
								if (!distance2.trim()) {
									$.scojs_message("请输入行驶里程",$.scojs_message.TYPE_ERROR);
									return false;
								} else if (Number(distance2) < 0|| Number(distance2) > 1000000) {
									$.scojs_message("行驶里程输入错误",$.scojs_message.TYPE_ERROR);
									return false;
								}
								if (!$('#buyTime2').val()) {
									$.scojs_message("请选择购车日期",$.scojs_message.TYPE_ERROR);
									return false;
								}
								var overhaulTime = $('#overhaulTime2').val();
								if (!overhaulTime) {
									$.scojs_message("请选择年检日期",$.scojs_message.TYPE_ERROR);
									return false;
								} else {
									if ((new Date(overhaulTime.replace("-","/")).getTime())- (new Date().getTime()) < 0) {
										$.scojs_message("年检日期不能在当前日期之前",$.scojs_message.TYPE_ERROR);
										return false;
									}
								}
								if (!$('#insuranceTime2').val()) {
									$.scojs_message("请选择保险到期日期",$.scojs_message.TYPE_ERROR);
									return false;
								}
								// 当监管平台开关打开时
								if (isUpPlatformInfo == 'true') {
									if (!$('#insurer2').val()) {
										$.scojs_message("上传监管平台开关已开启，请填写车辆保险公司",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#insuranceNo2').val()) {
										$.scojs_message("上传监管平台开关已开启，请填写车辆保险号",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#insType2').val()) {
										$.scojs_message("上传监管平台开关已开启，请填写车辆保险类型",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#insMoney2').val()) {
										$.scojs_message("上传监管平台开关已开启，请填写车辆保险金额",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#insValidTime2').val()) {
										$.scojs_message("上传监管平台开关已开启，请填写车辆保险生效日期",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#plateColor2').val()) {
										$.scojs_message("上传监管平台开关已开启，请填写车牌颜色，如蓝色",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#brand2').val()) {
										$.scojs_message("上传监管平台开关已开启，请填写车辆厂牌，如奔驰牌",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#vehicleType2').val()) {
										$.scojs_message("上传监管平台开关已开启，请填写车辆类型，如小型轿车",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#vehicleModel2').val()) {
										$.scojs_message("上传监管平台开关已开启，请填写车辆型号，如E643133",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#owner2').val()) {
										$.scojs_message("上传监管平台开关已开启，请填写所属车主",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#bodyColor2').val()) {
										$.scojs_message("上传监管平台开关已开启，请填写车身颜色",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#engineNo2').val()) {
										$.scojs_message("上传监管平台开关已开启，请填写发动机号",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#vin2').val()) {
										$.scojs_message("上传监管平台开关已开启，请填写车辆识别VIN",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#fuel2').val()) {
										$.scojs_message("上传监管平台开关已开启，请填写燃料类型，如：A（汽油）",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#permitted2').val()) {
										$.scojs_message("上传监管平台开关已开启，核定载客不能为空",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#registrationTime2').val()) {
										$.scojs_message("上传监管平台开关已开启，请填写车辆注册日期",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#displacement2').val()) {
										$.scojs_message("上传监管平台开关已开启，请填写车辆排量",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#address2').val()) {
										$.scojs_message("上传监管平台开关已开启，请填写详细地址",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#taxiAgency2').val()) {
										$.scojs_message("上传监管平台开关已开启，请填写运输证发证机构",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#taxiRegion2').val()) {
										$.scojs_message("上传监管平台开关已开启，请填写运输证经营区域",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#taxiNo2').val()) {
										$.scojs_message("上传监管平台开关已开启，请填写运输证编号",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#rateType2').val()) {
										$.scojs_message("上传监管平台开关已开启，运价类型不能为空 ",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#taxiRegisterTime2').val()) {
										$.scojs_message("上传监管平台开关已开启，请填写运输证激活日期",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#validTime2').val()) {
										$.scojs_message("上传监管平台开关已开启，请填写运输证有效日期",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#invalidTime2').val()) {
										$.scojs_message("上传监管平台开关已开启，请填写运输证失效日期",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#deviceSerial2').val()) {
										$.scojs_message("上传监管平台开关已开启，发票打印设备序号不能为空",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#deviceBrand2').val()) {
										$.scojs_message("上传监管平台开关已开启，定位装置品牌不能为空",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#deviceModel2').val()) {
										$.scojs_message("上传监管平台开关已开启，定位装置型号不能为空",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#devicegpsIMEI2').val()) {
										$.scojs_message("上传监管平台开关已开启，定位装置IMEI不能为空",$.scojs_message.TYPE_ERROR);
										return false;
									}
									if (!$('#deviceInstallTime2').val()) {
										$.scojs_message("上传监管平台开关已开启，定位装置安装日期不能为空",$.scojs_message.TYPE_ERROR);
										return false;
									}
									// 车辆照片是否为空
									var carImg2 = $("#carImg2")[0].src;
									var flag = carImg2.indexOf("noImg.png");
									if (CheckImgExists(carImg2)) {
										if (flag != -1) {
											$.scojs_message("上传监管平台开关已开启，请上传车辆照片",$.scojs_message.TYPE_ERROR);
											return false
										}
									} else {
										$.scojs_message("上传监管平台开关已开启，请上传车辆照片",$.scojs_message.TYPE_ERROR);
										return false
									}
								}
								$("#provincialStr2").val($("#provincial2").val()+ "-"+ $("#city2").val()+ "-"+ $("#county2").val());
								$.post(ctx+ "/zhuanche/updateCar",$("#submit_form_update").serialize(),function(data) {
									if (data.success) {
										$.scojs_message("修改成功",$.scojs_message.TYPE_OK);
										setTimeout(function() {
											window.location.reload(true);
										},800);
									} else {
										$.scojs_message(data.message,$.scojs_message.TYPE_ERROR);
									}
								},'json');
							}
						}, 'json');
					});

					$(".importCar").click(function() {
						$("#importCarModal").modal();
					});

					$("#carExcel").fileinput({
						uploadUrl : ctx + '/zhuanche/importCar', // you must set a valid URL here else you will get an error
						allowedFileExtensions : [ 'xls', 'xlsx' ],
						overwriteInitial : false,
						dropZoneEnabled : false,
						maxFileSize : 1024 * 10,
						maxFilesNum : 1,
						language : 'zh',
						dropZoneTitle : "拖拽文件到这里...",
						maxFileCount : 1, // 表示允许同时上传的最大文件个数
						enctype : 'multipart/form-data',
						allowedPreviewTypes : [ 'image' ],
						slugCallback : function(filename) {
							return filename.replace('(', '_').replace(']', '_');
						}
					}).on("fileuploaded",function(event, data) {
						$("#membermessageInfo").remove();
						var resp = data.response;
						var str = "<div id='zhuancheCarmessageInfo' style='margin:-10px 0px 5px 0;word-break:break-all;'";
						if (resp.success) {
							str += " class='alert alert-success'>";
							str += "导入完成:成功导入" + resp.rows+ "条";
							if (resp.carNumber != undefined&& resp.carNumber > 0) {
								str += ",车牌号重复:"+ resp.carNumber + "条";
							}
							if (resp.typeNumber != undefined&& resp.typeNumber > 0) {
								str += ",没有匹配的车型:"+ resp.typeNumber + "条";
							}
							$("#searchButton").click();
						} else {
							str += "class='alert alert-danger'>";
							if (resp.msg != undefined) {
								str += resp.msg;
							}
							if (resp.carNumberIndexs != undefined&& resp.carNumberIndexs.length > 0) {
								str += "在第["+ resp.carNumberIndexs.join(",")+ "]行记录,车牌号错误!   <br/>";
							}
							if (resp.drivingIndexs != undefined&& resp.drivingIndexs.length > 0) {
								str += "在第["+ resp.drivingIndexs.join(",")+ "]行记录,行驶公里数错误! <br/>";
							}
							if (resp.dateIndex != undefined&& resp.dateIndex.length > 0) {
								str += "在第["+ resp.dateIndex.join(",")+ "]行记录,时间格式错误!";
							}
						}
						str += "</div>";
						$(".kv-upload-progress").after(str);

					});
					/*
					 * $("#carExcel").on("filedeleted",function(event, data){
					 * console.debug("A");
					 * $("#zhuancheCarmessageInfo").remove(); });
					 * $("#carExcel").on("filesuccessremove",function(event,
					 * data, previewId, index){ alert("A");
					 * $("#zhuancheCarmessageInfo").remove(); });
					 * $("#carExcel").on("filepredelete",function(event, data,
					 * previewId, index){ alert("A");
					 * $("#zhuancheCarmessageInfo").remove(); });
					 */
					$("#carExcel").on("filecleared",function(event, data, previewId, index) {
						$("#zhuancheCarmessageInfo").remove();
					});

					$("#downloadTemplet").click(function() {
						var url = $(this).attr("url");
						var downloadFrame = $('#downloadFrame');
						if (downloadFrame.length == 0) {
							downloadFrame = $('<iframe id="downloadFrame" src="" style="display:none"></iframe>');
							$('body').append(downloadFrame);
						}
						downloadFrame.attr('src', url);
					});

					$(document).on("click",".checkCarItem",function() {
						var flag = $(this).attr("flag");
						if (Boolean.parse(flag)) {
							$(this).attr("flag", false);
							upload_car.remove(parseInt($(this).val()));
						} else {
							$(this).attr("flag", true);
							upload_car.push(parseInt($(this).val()));
						}
						$(".query-jiant").append(
										"<span class='label label-info checkItemInfoLable' style='position: absolute;right: 80px;margin-top: 10px;'>共选中 : "
												+ upload_car.length
												+ "条</span>");
						// event.stopPropagation();
					});
					var reportTime;
					$(".report").bind("click",function() {
						if (isUpPlatformInfo == 'false') {
							$.scojs_message("监管平台上传开关已关闭",$.scojs_message.TYPE_ERROR);
							return;
						}

						$("#progressDiv").show();
						$("#upCar .modal-body .tempInfo")
								.remove();
						$("#upCar .modal-dialog").addClass(
								"modal-sm");
						$("#loading").width(10);
						/*
						 * var item =
						 * $(".checkCarItem[flag=true]"); var
						 * ids =[]; for(var i=
						 * 0,id;id=item[i++];){
						 * ids.push($(id).val()); }
						 * if(ids.length==0){ return false; }
						 */
						if (upload_car.length == 0) {
							$.scojs_message("请选择需要上报的车辆",$.scojs_message.TYPE_ERROR);
							return false;
						}

						var param = upload_car.join(",")
							$.post(ctx+ "/zhuanche/checkCarInfo",{ids : param},function(data) {
								if (data.code == 0) {
									$("#dataCheck").text("数据校验中");
									$("#upCar").modal();
									reportTime = window.setInterval(function() {
										var temp = $("#progressDiv").width();
										var load = $("#loading").width();
										if (load >= temp) {
											$("#loading").width(10);
										} else {
											$("#loading").width(load + 10);
										}
									},500);
									var tempInterval = window.setInterval(function() {
										$.get(ctx+ "/zhuanche/getCheckCarInfo",{},function(data) {
											if (data != null) {
												if (data.success) {
													var str = "<p class='tempInfo'>";
													var count = 1;
													for ( var key in data) {
														if (key != "success") {
															str += count+ "：["+ key+ "]<br/>";
															var daataStr = data[key].split(',');
															for(var i=0;i<daataStr.length-1;i++){
																str += "&nbsp&nbsp&nbsp&nbsp"+count+"."+(i+1)+"："+daataStr[i]+"<br/>";
															}
															count++;
														}
													}
													clearInterval(reportTime);
													clearInterval(tempInterval);
													$("#progressDiv").hide();
													str += "</p>"
													if (count == 1) {
														$("#upCar .modal-body").html("数据校验通过，是否上传？&nbsp<button type='button' id='upCarInfoButton' class='btn btn-info btn-sm' style='padding:2px 10px;''>上传</button>");
													} else {
														$("#upCar .modal-dialog").removeClass("modal-sm");
														$("#upCar .modal-body").html("");
														$("#dataCheck").text("数据校验结果：共计"+(count-1)+"辆车校验失败");
														$("#upCar .modal-body").append(str);
													};
												}
											}
										})

									},1000);
								}
							});
						});

					// 上报全部车辆
					$(document).on("click",".reportAll",function() {
						if (!isUpPlatformInfo == 'true') {
							$.scojs_message("监管平台上传开关已关闭",$.scojs_message.TYPE_ERROR);
							return;
						}
						thisButton = $(this);
						var confirm = $.scojs_confirm({
							content : "<P style='color: red' >你确定要上报所有车辆信息至监管平台吗？</P>",
							action : reportAllCar
						});
						confirm.show();
					});
					var reportAllTime;
					function reportAllCar() {
						$("#progressDiv").show();
						$("#upCar .modal-body .tempInfo").remove();
						$("#upCar .modal-body .spanInfo").remove();
						$("#upCar .modal-dialog").addClass("modal-sm");
						$("#loading").width(10);
						$.post("${ctx}/zhuanche/reportAll",{},
							function(data) {
								if (data.code == 0) {
									$("#upCar").modal();
									reportAllTime = window.setInterval(function() {
										var temp = $("#progressDiv").width();
										var load = $("#loading").width();
										if (load >= temp) {
											$("#loading").width(10);
										} else {
											$("#loading").width(load + 10);
										}
									}, 500);
									var tempInterval = window.setInterval(function() {
											$.get(ctx+ "/zhuanche/getCheckCarAllInfo",{},function(data) {
												if (data != null) {
													if (data.reportAllCheck) {
														var str = "<p class='tempInfo'>";
														var count = 0;
														var temp = data.reportAllData;
														for ( var key in temp) {
															str += "["+ key+ "],"
															count++;
														}
														str += " 数据验证不通过,请仔细检查!"
														clearInterval(reportAllTime);
														clearInterval(tempInterval);
														$("#progressDiv").hide();
														str += "</p>"
														if (count == 0) {
															$("#upCar .modal-body").html("<span class='spanInfo'数据校验通过，是否上传？&nbsp<button type='button' id='upCarAllInfoButton' class='btn btn-info btn-sm' style='padding:2px 10px;''>上传</button></span>");
														} else {
															$("#upCar .modal-body").html("");
															$("#upCar .modal-dialog").removeClass("modal-sm");
															$("#upCar .modal-body").append(str);
														};
													}
												}
											})
										}, 1000);
									}
								})
							}
					
					var upcarall;
					$(document).on("click","#upCarAllInfoButton",function() {
						$.post("${ctx}/zhuanche/#upCarAll",{},function(data) {
							if (data.code == 0) {
								upcarall = window.setInterval(function() {
									$.get("${ctx}/zhuanche/getupCarAllInfo",{},function(data) {
										if (data.success) {
											$.scojs_message("成功:"+ ata.upCarAllInfo.successNum+ "  失败:"+ data.upCarAllInfo.failNum,$.scojs_message.TYPE_OK);
										} else {
											$.scojs_message("失败",$.scojs_message.TYPE_ERROR);
										}
									})
								},1000);
							}
						});
					})

					$(document).on("click","#upCarInfoButton",function() {
						$("#upCar").modal("hide");

						$("#waitUpCar .modal-body").html("<img src='/assets/img/hook-spinner.gif'/>");
						$("#waitUpCar").modal();

						var item = $(".checkCarItem[flag=true]");
						var ids = [];
						for (var i = 0, id; id = item[i++];) {
							ids.push($(id).val());
						}
						if (ids.length == 0) {
							return false;
						}
						var param = ids.join(",");
						var thisButton = $(this);
						thisButton.attr("disabled", true);
						$.post(ctx+ "/zhuanche/upCarInfo",{ids : param},function(data) {
							if (data.code == 0) {
								$("#waitUpCar").modal("hide");
								$.scojs_message("上传成功",$.scojs_message.TYPE_OK);

							} else {
								$.scojs_message("上传失败",$.scojs_message.TYPE_ERROR);
								$("#waitUpCar").modal("hide");
							}
							var s = window.setInterval(function() {
								location.reload();
							},1000);
						});
					})

					$(document).ready(function() {
						$(document).on("click", ".zhuijia", function() {
							/*
							 * var id = $(this).attr("orderId");
							 * window.open(ctx+"/zhuanche/addmore/"+id,"_blank","height=850,width=1220,scrollbars=yes,resizable=yes,alwaysRaised=yes,toolbar=no");
							 * }); window.setInterval(function(){
							 * initdata(false); },1000*60);
							 */
							$.scojs_message("调用", $.scojs_message.TYPE_ERROR);
						});
					})

				});

function initdata(isappend) {
	$.ajax({
		url : ctx + '/zhuanche/carsData',
		type : 'GET',
		async : false,
		dataType : 'json',
		data : {
			"carNumber" : search_carNumber,
			"employName" : search_employName,
			"company" : search_companyName,
			"carType" : search_carType,
			"status" : search_status,
			"isPlatform" : search_isPlatform,
			"pager.offset" : offset,
			"limit" : limit,
		},
		success : function(result) {
			$("#all").attr("val", "false");
			$("#all").removeAttr("checked");
			var newcontent = '';
			var totalElements = result.totalElements;
			if (totalElements > 0) {
						$(result.content).each(function(index, data) {
											var statusColor = "";
											var mainButton = "";
											var selectButton = "";
											statusColor = "btn-primary";
											mainButton = "<a href='#' data-value='"+ data.id+ "' class='btn btn-primary btn-xs updatethis'>编辑</a>";
											if(jgpt){
												selectButton += "<li style='cursor:pointer;'><a href='"+ ctx+ "/zhuanche/"+ data.id+ "/insurance'  data-value='"+ data.id
												+ "'  class='btn-link '>保险</a></li>";
											}
											selectButton += "<li style='cursor:pointer;'><a href='#'  data-value='"+ data.id+ "'  class='btn-link deletethis'>删除</a></li>";
											var allButton = "<div class='btn-group'>"+ mainButton
													+ "<button type='button' class='btn "+ statusColor+ " btn-xs dropdown-toggle' data-toggle='dropdown' aria-expanded='false'>"
													+ "<span class='caret'></span>"
													+ "<span class='sr-only'>Toggle Dropdown</span>"
													+ "</button>"
													+ "<ul class='dropdown-menu' role='menu'>"+ selectButton+ "</ul>"
													+ "</div>";

											var status = "";
											if (data.status == 0) {
												status = "<span class='label label-info'>空闲</span>";
											} else if (data.status == 1) {
												status = "<span class='label label-warning'>忙碌</span>";
											} else if (data.status == 2) {
												status = "<span class='label label-danger'>离线 </span>";
											}
											var limitLine = "";
											if (data.limitLine == 1) {
												limitLine = "星期一";
											} else if (data.limitLine == 2) {
												limitLine = "星期二";
											} else if (data.limitLine == 3) {
												limitLine = "星期三";
											} else if (data.limitLine == 4) {
												limitLine = "星期四";
											} else if (data.limitLine == 5) {
												limitLine = "星期五";
											} else {
												limitLine = "不限行";
											}

											var inArray = $.inArray(data.id,
													upload_car);
											var checked = "";
											if (inArray != -1) {
												checked = "checked='checked'";
											}

											var upStatus = "";
											if (data.upStatus == 1) {
												upStatus = "√";
											}
											if(jgpt){
												var checkString =
													"<td>"
														+ "<div class='checkbox checkbox-success '   style='margin-top:-5px;margin-left: 40px;'>"
														+ "<input id='"+ data.id+ "' class='styled checkCarItem' flag='false' value='"+ data.id+ "' type='checkbox' "+ checked+ " />"
														+ "<label for='"+ data.id+ "'>"+ "</label>"
														+ "</div>"
													+ "</td>"
												var upStatusString =  "<td style = 'text-align: center'>"+ upStatus+ "</td>"
											}
											newcontent += "<tr class='grid-data tr-hui'  >"
													+ checkString
													+ "<td>"+ status+ "</td>"
													+ upStatusString
													+"<td>"+data.carNumber+(data.flights!=null?"("+data.flights+")":"")+"</td>"
													+ "<td>"+ data.carName+ "</td>"
													+ (isMainCompany ? ("<td>"+ data.companyName + "</td>"): "")
													+ "<td>"+ (data.carTypeName != null ? data.carTypeName: "")+ "</td>"
													+ "<td>"+ (data.employName == null|| data.employName == "" ? "--": data.employName)+ "</td>"
													+ "<td>"+ limitLine+ "</td>"
													+
													// "<td "+((new
													// Date(data.insuranceTime).getTime()-(1000*60*60*24*30))-new
													// Date().getTime()<0?"style='color:#D9534F'":"")+">"+(new
													// Date(data.insuranceTime).Format("yyyy-MM-dd"))+"</td>"+
													"<td "+ ((new Date(data.overhaulTime).getTime() - (1000 * 60 * 60 * 24 * 30))- new Date().getTime() < 0 ? "style='color:#D9534F'": "")+ ">"
														+ (new Date(data.overhaulTime).Format("yyyy-MM-dd"))
													+ "</td>"
													+ "<td>"+ data.drivingDistance+ "</td>"
													+ "<td>"+ data.provincial+ data.city+ data.county+ data.address
													+ "</td>"
													+ "<td>"+ allButton+ "</td>"
													+ "</tr>";
										});
					}
					if (!isappend) {
						$("#car_tbody .grid-data").remove();
					}
					$("#car_tbody").append(newcontent);
					$("#car_tbody .grid-data").each(function(index, element) {
						$(element).fadeIn(200 + (index * 35));
					});
					if (!hasShowPages) {
						if (!isappend) {
							currentTotalElements = result.content.length;
							pageLoading = true;
						} else {
							if (currentTotalElements < totalElements) {
								currentTotalElements += result.content.length;
								pageLoading = true;
							} else {
								pageLoading = false;
							}
						}
					}

					if (hasShowPages) {
						firstLoadPages = true;
						$(".pageshow li a").html("共" + totalElements + " 条数据");
						$(".pagedata").paging(totalElements,{
							format : '[< ncnnn >]',
							perpage : pagingLimit,
							page : (offset / limit) + 1,
							onSelect : function(page) {
								limit = pagingLimit;
								offset = (page - 1) * limit;
								if (!firstLoadPages) {
									initdata(false);
								}
								firstLoadPages = false;
								return false;
							},
							onFormat : function(type) {
								switch (type) {
								case 'block': // n and c
									if (!this.active)
										return '';
									else if (this.value != this.page)
										return '<li><a href="#">'
												+ this.value
												+ '</a></li>';
									return '<li class="active"><a href="#">'
											+ this.value
											+ '</a></li>';
								case 'next': // >
									return '<li><a href="#">&rsaquo;</a><li>';
								case 'prev': // <
									return '<li><a href="#">&lsaquo;</a><li>';
								case 'first': // [
									return '<li><a href="#">&laquo;</a><li>';
								case 'last': // ]
									return '<li><a href="#">&raquo;</a></li>';
								}
							}
						});
					} else {
						pageLoading = false;
						hasShowPages = true;
						$(".pageshow").show();
						$(".pagedata").show();
						$(".pageshow li a").html("共" + totalElements + " 条数据");
						$(".pagedata").paging(totalElements,{
							format : '[< ncnnn >]',
							perpage : pagingLimit,
							onSelect : function(page) {
								limit = pagingLimit;
								offset = (page - 1) * limit;
								if (!firstLoadPages) {
									initdata(false);
								}
								firstLoadPages = false;
								return false;
							},
							onFormat : function(type) {
								switch (type) {
								case 'block': // n and c
									if (!this.active)
										return '';
									else if (this.value != this.page)
										return '<li><a href="#">'
												+ this.value
												+ '</a></li>';
									return '<li class="active"><a href="#">'
											+ this.value
											+ '</a></li>';
								case 'next': // >
									return '<li><a href="#">&rsaquo;</a><li>';
								case 'prev': // <
									return '<li><a href="#">&lsaquo;</a><li>';
								case 'first': // [
									return '<li><a href="#">&laquo;</a><li>';
								case 'last': // ]
									return '<li><a href="#">&raquo;</a></li>';
								}
							}
						});
					}
				}
			});
};

Date.prototype.Format = function(fmt) {
	var o = {
		"M+" : this.getMonth() + 1, // 月份
		"d+" : this.getDate(), // 日
		"h+" : this.getHours(), // 小时
		"m+" : this.getMinutes(), // 分
		"s+" : this.getSeconds(), // 秒
		"q+" : Math.floor((this.getMonth() + 3) / 3), // 季度
		"S" : this.getMilliseconds()
	// 毫秒
	};
	if (/(y+)/.test(fmt))fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for ( var k in o)
		if (new RegExp("(" + k + ")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]): (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
};
Boolean.parse = function(str) {
	switch (str.toLowerCase()) {
	case "true":
		return true;
	case "false":
		return false;
	default:
		throw new Error("Boolean.parse: Cannot convert string to boolean.");
	}
};
Array.prototype.remove = function(val) {
	if (val == null) {
		return false;
	}
	var flag = false;
	for (var i = 0, n = 0; i < this.length; i++) {
		if (this[i] != val) {
			this[n++] = this[i];
		} else {
			flag = true;
		}
	}
	if (flag) {
		this.length -= 1;
	}
};
function CheckImgExists(imgurl) {
	var ImgObj = new Image(); // 判断图片是否存在
	ImgObj.src = imgurl;
	// 没有图片，则返回-1
	if (ImgObj.fileSize > 0 || (ImgObj.width > 0 && ImgObj.height > 0)) {
		return true;
	} else {
		return false;
	}
};
